< previous page page_345 next page >

Page 345
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdResume_Click()
   ' Demonstrate the Resume statement.

   On Error GoTo Error_EH

   ' Attempt to open a file on Drive A:
   MsgBox "The length of the file is: " & _
          FileLen(txtFileLen)

NormalExit:
   Exit Sub

Error_EH:
   Select Case Err.Number
      Case 5      ' Invalid parameter.
         If MsgBox( _
            "The floppy disk drive isn't ready. Try again?", _
            vbQuestion + vbYesNo) = vbYes Then
             Resume
         Else
             Resume NormalExit
         End If

      Case Else
         ' Call a generic last-ditch error handler
         Call ErrorHandler

         Resume NormalExit
   End Select
End Sub
In this code, you check the Err.Number property to see whether its the error number youre looking for. If its 5, you know you need to prompt the user to input the floppy disk into the drive. In the MsgBox() function, you ask the user whether he or she wants to attempt the operation again. If the answer is Yes, you can perform a Resume to retry the operation. If the answer is No, you can simply resume to the normal exit routine. You should always give the user a way to cancel out of the procedure; otherwise, he or she may end up in an endless loop.

 
< previous page page_345 next page >

If you like this book, buy it!